<?php
/**
 * @date 2012-3-19
 * @author GhostWei
 */
class controller_admin_account extends system_controller_action {
	
	private $user = null;
	private $referer = null;
	private $language = null;
	public function init() {
		if($this->adminLogin()) {
			return;
		}
		$this->view->language = $this->language = $this->view->language();
		$this->view->user = $this->user = system_user::getInstance()->getRow();
		$this->referer = $this->getRequest()->getHeader('referer');

		$page = new system_nav_page();
		$page->setLabel($this->language->load('My Account'))->setRoute('account/index');
		$this->crumbs()->addPage($page);
	}
	
	public function action_index() {
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Review'))->setRoute('account/index');
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_email() {		
		if($this->getRequest()->isPost()) {
			try {
				$password = trim($this->getRequest()->getPost('password'));
				$email = trim($this->getRequest()->getPost('email'));
				if(!$this->user->auth(md5($password))) {
					throw new system_exception('The Password is wrong!');
				}
				$concept = new system_passport_concept();
				$row = $concept->fetch(array('passport.id = ?' => $this->user->getPassportId()));
				$row->setEmail($email);
				$row->save();

				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Change Email'))->setRoute('account/email');
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_setting() {
		if($this->getRequest()->isPost()) {
			try {
				$name = trim($this->getRequest()->getPost('name'));					
				$row = $this->user;
				$row->setNickname($name);
				$row->save();
				$this->view->throwMessage = 'Complete';
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$page = new system_nav_page();
		$page->setLabel($this->language->load('My Profile'))->setRoute('account/setting');
		$this->crumbs()->addPage($page);
		$this->render();
	}
	
	public function action_password() {		
		if($this->getRequest()->isPost()) {
			try {
				$password = trim($this->getRequest()->getPost('oldpassword'));
				$password1 = trim($this->getRequest()->getPost('newpassword'));
				$password2 = trim($this->getRequest()->getPost('newpassword1'));

				if(strlen($password1) < 6 || strlen($password2) < 6) {
					throw new system_exception('New Password length is too short!');
				}
				if($password1 != $password2) {
					throw new system_exception('New Password confirmation does not match!');
				}		
				if(!$this->user->auth(md5($password))) {
					throw new system_exception('The old password is wrong!');
				}
				$concept = new system_passport_concept();
				$row = $concept->fetch(array('passport.id = ?' => $this->user->getPassportId()));
				$row->setCredential($password1);
				$row->save();

				$this->view->throwMessage = 'Complete';				
				system_passport::getInstance()->getStorage()->clean();
			} catch (Exception $e) {
				$this->view->throwException = $e;
			}
		}
		$page = new system_nav_page();
		$page->setLabel($this->language->load('Change Password'))->setRoute('account/password');
		$this->crumbs()->addPage($page);
		$this->render();
	}

}